package com.yocard.yxyd.ui.activity;

import android.support.v4.widget.NestedScrollView;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.gyf.barlibrary.ImmersionBar;
import com.umeng.analytics.MobclickAgent;
import com.yocard.yxyd.R;
import com.yocard.yxyd.adapter.TopupAadapter;
import com.yocard.yxyd.base.AppContext;
import com.yocard.yxyd.base.Constants;
import com.yocard.yxyd.http.model.response.CommonBean;
import com.yocard.yxyd.http.model.response.RechargesBean;
import com.yocard.yxyd.ui.activity.base.BaseActivity;
import com.yocard.yxyd.ui.contract.TopUpContract;
import com.yocard.yxyd.ui.presenter.TopupPresenter;
import com.yocard.yxyd.utils.BigDecimalUtil;
import com.yocard.yxyd.utils.util.ActivityUtils;
import com.yocard.yxyd.utils.util.ConvertUtils;
import com.yqritc.recyclerviewflexibledivider.HorizontalDividerItemDecoration;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created on 2019/4/9.
 * 充值中心 19/05/04
 */
public class TopupCenterActivity extends BaseActivity<TopupPresenter> implements TopUpContract.View {

    @BindView(R.id.tv_right)
    TextView tvRight;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.scroll_view)
    NestedScrollView nestedScrollView;
    @BindView(R.id.recycle_view)
    RecyclerView recyclerView;
    @BindView(R.id.tv_need_pay)
    TextView tvNeedPay;
    @BindView(R.id.tv_balance)
    TextView tvBalance;

    protected TopupAadapter mAdapter;

    @Override
    protected void initInject() {
        getActivityComponent().inject(this);
    }

    @Override
    protected int getLayout() {
        return R.layout.act_top_up;
    }

    @Override
    protected void initEventAndData() {
        initView();
        initData();
    }

    private void initData() {
        showLoadingDialog();
        mPresenter.getBalance();
        mPresenter.getRecharges();
    }

    private void initView() {
        initTitle();
        HorizontalDividerItemDecoration horizontalBuild = new HorizontalDividerItemDecoration.Builder(mContext)
                .color(AppContext.getColor_(R.color.white))
                .size(ConvertUtils.dp2px(15))
                .build();

        recyclerView.setNestedScrollingEnabled(false);
        GridLayoutManager gridLayoutManager = new GridLayoutManager(mContext, 3);
        recyclerView.setLayoutManager(gridLayoutManager);
        recyclerView.addItemDecoration(horizontalBuild);
    }

    private void initTitle() {
        ImmersionBar.with(this).statusBarColor(R.color.white)
                .statusBarDarkFont(true).navigationBarColor(R.color.white).init();
        tvRight.setText("交易记录");
        tvRight.setVisibility(View.VISIBLE);
        tvTitle.setText("充值中心");
    }

    @OnClick({R.id.iv_back, R.id.tv_right, R.id.tv_tips, R.id.tv_pay_now})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                ActivityUtils.finishActivity(this);
                break;
            case R.id.tv_right:
                ActivityUtils.startActivity(TransactionActivity.class);
                break;
            case R.id.tv_tips:
                ActivityUtils.startActivity(TopupTipsActivity.class);
                break;
            case R.id.tv_pay_now:
                //防止短时间重复点击
                if (isFastDoubleClick()) return;

                MobclickAgent.onEvent(mContext,"10","click_topup_button");
                if(mAdapter != null){
                    mPresenter.createOrder(this,mAdapter.getSelectId());
                }
                break;
        }
    }

    @Override
    public void getRechargesSuccess(List<RechargesBean> rechargesBeans) {
        dismisDialog();
        mAdapter = new TopupAadapter(rechargesBeans);
        recyclerView.setAdapter(mAdapter);
        tvNeedPay.setText(mAdapter.getSelectedAmount());
        mAdapter.setOnItemclick(amount -> {
            tvNeedPay.setText(mAdapter.getSelectedAmount());
        });
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onFreshEvent(CommonBean bean) {
        if (bean.getCode() == Constants.CODE_REFRESH) {
            mPresenter.getBalance();
        }
    }

    @Override
    public void onGetBalance(String balance) {
        dismisDialog();
        tvBalance.setText(BigDecimalUtil.getFormatCoin(balance));
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }
}
